﻿using GalaSoft.MvvmLight;
using MvvmLight1.Model;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System;

namespace MvvmLight1.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// See http://www.mvvmlight.net
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        private List<Tree> mytree = new List<Tree>();
        public List<Tree> MyTree
        {
            get
            {
                return mytree;
            }
        }
        public MainViewModel()
        {
            using (energybasedbEntities entity = new energybasedbEntities())
            {
                entity.Database.Connection.Open();
                var treeList = (from c in entity.treestruct select c).ToList();
                var treeRoot = (from c in entity.treestruct where c.ParentID < 0 select c).FirstOrDefault();
                Tree tree = new Tree();
                BuildTree(tree, -1, treeList);
                this.mytree.Add(tree.Items.First());
            }
        }

        private void BuildTree(Tree tree, int id, List<treestruct> treeList)
        {
            var childs = from c in treeList where c.ParentID == id select c;
            if (childs.Count() == 0)
                return;
            foreach (var child in childs)
            {
                Tree node = new Tree();
                node.Id = child.ID;
                node.Meterid = child.MeterID == null ? -1 : (int)child.MeterID;
                node.Name = child.Name;
                node.Parentid = child.ParentID;
                node.Type = child.Type;
                tree.Items.Add(node);
                BuildTree(node, node.Id,treeList);
            }
        }
    }
    public class Tree
    {
        int id;
        string name;
        int parentid;
        int meterid;
        int type;
        List<Tree> items;

        public int Id
        {
            get
            {
                return id;
            }

            set
            {
                id = value;
            }
        }

        public string Name
        {
            get
            {
                return name;
            }

            set
            {
                name = value;
            }
        }

        public int Parentid
        {
            get
            {
                return parentid;
            }

            set
            {
                parentid = value;
            }
        }

        public int Meterid
        {
            get
            {
                return meterid;
            }

            set
            {
                meterid = value;
            }
        }

        public int Type
        {
            get
            {
                return type;
            }

            set
            {
                type = value;
            }
        }

        public List<Tree> Items
        {
            get
            {
                if (items == null)
                    items = new List<Tree>();
                return items;
            }

            set
            {
                items = value;
            }
        }
    }
}